/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2019年6月23日
 * <修改描述:>
 */
package com.tx.component.social.configuration;

import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import com.tx.component.social.SocialConstants;
import com.tx.component.social.dao.UserSocialAccountDao;
import com.tx.component.social.dao.impl.UserSecurityAccountDaoMybatisImpl;
import com.tx.component.social.service.UserSocialAccountService;
import com.tx.component.social.support.gitee.GELoginPlugin;
import com.tx.component.social.support.github.GHLoginPlugin;
import com.tx.component.social.support.weibo.WBLoginPlugin;
import com.tx.core.exceptions.util.AssertUtils;
import com.tx.core.mybatis.support.MyBatisDaoSupport;
import com.tx.core.starter.component.ComponentConstants;

/**
 * 权限容器自动配置项<br/>
 * <功能详细描述>
 * 
 * @author  Administrator
 * @version  [版本号, 2019年6月23日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Configuration
public class SocialContextPersisterConfiguration {
    
    /**
     * mybatis持久层逻辑实现<br/>
     * <功能详细描述>
     * @author  Administrator
     * @version  [版本号, 2019年5月2日]
     * @see  [相关类/方法]
     * @since  [产品/模块版本]
     */
    @Configuration
    @ConditionalOnProperty(prefix = ComponentConstants.PERSISTER_PROPERTIES_PREFIX, value = "type", havingValue = "mybatis")
    @ConditionalOnBean(name = { "tx.component.myBatisDaoSupport" })
    public static class MybatisAuthContextPersisterConfiguration
            implements InitializingBean {
        
        /** mybatis属性 */
        @Resource(name = "tx.component.myBatisDaoSupport")
        private MyBatisDaoSupport myBatisDaoSupport;
        
        /** 事务管理器 */
        @SuppressWarnings("unused")
        private TransactionTemplate transactionTemplate;
        
        /** <默认构造函数> */
        public MybatisAuthContextPersisterConfiguration(
                PlatformTransactionManager transactionManager) {
            this.transactionTemplate = new TransactionTemplate(
                    transactionManager);
        }
        
        /**
         * @throws Exception
         */
        @Override
        public void afterPropertiesSet() throws Exception {
            AssertUtils.notNull(this.myBatisDaoSupport,
                    "myBatisDaoSupport is null.");
        }
        
        /**
         * 权限类型项持久层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return AuthTypeItemDao [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(name = SocialConstants.BEAN_NAME_USER_SOCIAL_ACCOUNT_DAO)
        public UserSocialAccountDao authTypeItemDao() {
            UserSecurityAccountDaoMybatisImpl dao = new UserSecurityAccountDaoMybatisImpl();
            dao.setMyBatisDaoSupport(this.myBatisDaoSupport);
            return dao;
        }
        
        /**
         * 权限类型项业务层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return AuthTypeItemService [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(SocialConstants.BEAN_NAME_USER_SOCIAL_ACCOUNT_SERVICE)
        public UserSocialAccountService authTypeItemService(
                UserSocialAccountDao userSocialAccountDao) {
            UserSocialAccountService service = new UserSocialAccountService(
                    userSocialAccountDao);
            return service;
        }
    }
    
    /**
     * github登陆插件<br/>
     * <功能详细描述>
     * @return [参数说明]
     * 
     * @return GHLoginPlugin [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    @ConditionalOnProperty(prefix = "tx.component.security.social", value = "github", havingValue = "true")
    @Bean("social.ghLoginPlugin")
    public GHLoginPlugin ghLoginPlugin() {
        GHLoginPlugin ghLoginPlugin = new GHLoginPlugin();
        return ghLoginPlugin;
    }
    
    /**
     * github登陆插件<br/>
     * <功能详细描述>
     * @return [参数说明]
     * 
     * @return GELoginPlugin [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    @ConditionalOnProperty(prefix = "tx.component.security.social", value = "gitee", havingValue = "true")
    @Bean("social.geLoginPlugin")
    public GELoginPlugin geLoginPlugin() {
        GELoginPlugin geLoginPlugin = new GELoginPlugin();
        return geLoginPlugin;
    }
    
    /**
     * github登陆插件<br/>
     * <功能详细描述>
     * @return [参数说明]
     * 
     * @return GELoginPlugin [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    @ConditionalOnProperty(prefix = "tx.component.security.social", value = "weibo", havingValue = "true")
    @Bean("social.wbLoginPlugin")
    public WBLoginPlugin wbLoginPlugin() {
        WBLoginPlugin wbLoginPlugin = new WBLoginPlugin();
        return wbLoginPlugin;
    }
    
}
